<?php

class Sondage_GsondageController extends Zend_Controller_Action
{

    public function init()
    {
    	
    	
        /* Initialize action controller here */
    	parent::init();
    	if (Zend_Auth::getInstance()->hasIdentity()) {
    		echo "already logged in as: " ;//. Zend_Auth::getInstance()->getIdentity();
    		$identity=Zend_Auth::getInstance()->getIdentity();
    		
    		/* Initialize action controller here */
    		if($identity->role!="admin"){
    			$this->_helper->redirector('login',"authentification","sondage");
    		}
    	}else{
    		$this->_helper->redirector('authentification','gparticipants','sondage');
    	}
    	 
    }

    public function indexAction()
    {
        // action body
    }

    public function addsondageAction()
    {
        // action body
        
    	$sondageform = new Application_Form_Addsondage();
    	$this->view->form = $sondageform;
   	
    	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		if ($sondageform->isValid($formData)) {
    			$sondage = new Application_Model_DbTable_Sondage();
    			
    			$datestamp=new Zend_Date();
    			
    			$row = $sondage->createRow();
    			 
    			$identity=Zend_Auth::getInstance()->getIdentity();
    			
    			$row->sondagepersonne_idsondagepersonne = $identity->idsondagepersonne;
    			
    			$row->titre = $sondageform->getValue('titre');
    			
    			$row->titre = $sondageform->getValue('titre');
    			$row->description = $sondageform->getValue('description');
    			$row->date_creation = $datestamp->get(zend_date::TIMESTAMP);
    			
    			$datestamp->set($sondageform->getValue('date_publication'));
    			$row->date_publication = $datestamp->get(zend_date::TIMESTAMP);
    			
    			$row->domaine_iddomaine = $sondageform->getValue('domaine_iddomaine');
    			
    			
    			$result = $row->save();
    			
    			 
    			$this->_helper->redirector("gestionsondage");
    		}
    		 
    	}
    	
    	
    	
    	
    }

    public function sondageAction()
    {
        // action body
    }

    public function gestionsondageAction()
    {
        // action body
        
    	//chargement des donnée de la base de donnée
    	$mapper = new Application_Model_Mapper_Sondage();
    	     	 
    	$select=$mapper->fetchAllToArray();
    	//ajouter le requtte a la paginateur
    	$paginator = Zend_Paginator::factory($select);
    	//ajouter le nombre d'enregistrement par page
    	$paginator->setItemCountPerPage(3);
    	//ajouter la page par defaut
    	$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    	//ajouter le paginateur au view
    	$this->view->paginator = $paginator;
    	
    	
    	$headercolums=array(
    			"titre"=> "Titre",
    			"domaine_iddomaine"=>"Domaine",
    			"date_creation"=>"date de création",
    			"date_publication"=>"date de publication"
    			
    			
    			);
   $this->view->headercolums=$headercolums;
    	
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    	
    }

    public function editerquestionAction()
    {
        // action body
        
    	
    	
    	//chargement des donnée de la base de donnée
    	$mapper = new Application_Model_DbTable_Question();
    	 
    	$select=$mapper->fetchAll(array("sondage_idsondage = ?"=>$this->getRequest()->getParam("id")))->toArray();
    	//ajouter le requtte a la paginateur
    	 
    	
    	$paginator = Zend_Paginator::factory($select);
    	//ajouter le nombre d'enregistrement par page
    	$paginator->setItemCountPerPage(3);
    	//ajouter la page par defaut
    	$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    	//ajouter le paginateur au view
    	$this->view->paginator = $paginator;
    	
    	
    	$headercolums=array(
    			"ordrequestion"=>"Ordre",
    			"textquestion"=> "Question",
    			"description"=>"Description",
    			"score"=>"Nombre de reponse juste"
    	
    	);
    	$this->view->headercolums=$headercolums;
    	
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    	
    	//********************************************Ajouter et enregistrer*****************
    	
    	$questionform = new Application_Form_Editerquestions();
    	$this->view->form = $questionform;
    	
    	 
    	 
    	 
    	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		if ($questionform->isValid($formData)) {
    			$question = new Application_Model_DbTable_Question();
    			 
    		
    			 
    			$row = $question->createRow();
    			
    			$row->sondage_idsondage=$this->getRequest()->getParam("id");
    		
    			$row->typequestion_idtypequestion = $questionform->getValue('typequestion_idtypequestion');
    			$row->textquestion = $questionform->getValue('textquestion');
    			 
    			
    			$row->description = $questionform->getValue('description');
    			 
    			 
    			 
    			$row->ordrequestion = $questionform->getValue('ordrequestion');
    			$row->score = $questionform->getValue('score');
    			 
    			
    	
    			$result = $row->save();
    	
    			//	$questionform->reset();
    	
    			$this->_helper->redirector("editerquestion","gsondage","sondage",array("id"=>$this->getRequest()->getParam("id")));
    		}
    		 
    	}
    	
	
    }

    public function deletesondageAction()
    {
        // action body
    }

    public function updatesondageAction()
    {
        // action body
    }

    public function editerrepenseAction()
    {
        // action body
        
    	//chargement des donnée de la base de donnée
    	$mapper = new Application_Model_DbTable_Reponse();
    	
    	$select=$mapper->fetchAll(array("Question_idQuestion = ?"=>$this->getRequest()->getParam("id")))->toArray();
    	//ajouter le requtte a la paginateur
    	
    	 
    	$paginator = Zend_Paginator::factory($select);
    	//ajouter le nombre d'enregistrement par page
    	$paginator->setItemCountPerPage(3);
    	//ajouter la page par defaut
    	$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    	//ajouter le paginateur au view
    	$this->view->paginator = $paginator;
    	 
    	 
    	$headercolums=array(
    			"labelrepense"=>"Reponse",
    			"repensevrai"=> "Reponse juste",
    			"typereponse_idtypereponse"=>"Type de reponse",
    			"score"=>"Score"
    			 
    	);
    	$this->view->headercolums=$headercolums;
    	 
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    	 
    	//********************************************Ajouter et enregistrer*****************
    	 
    	$reponseform = new Application_Form_Editerreponse();
    	$this->view->form = $reponseform;
    	 
    	print_r($this->_request->getPost());
    	
    	
    	if ($this->_request->isPost()) {
    		$formData = $this->_request->getPost();
    		
    		
    		if ($reponseform->isValid($formData)) {
    			$reponse = new Application_Model_DbTable_Reponse();
    	
    			$row = $reponse->createRow();
    			 
    			$row->Question_idQuestion=$this->getRequest()->getParam("id");
    	
    			$row->typereponse_idtypereponse= $reponseform->getValue('typereponse_idtypereponse');
    			$row->labelrepense = $reponseform->getValue('labelrepense');
    			 
    			$row->repensevrai = $reponseform->getValue('repensevrai');
    		
    			$row->score = $reponseform->getValue('score');
    			$row->descriptionhelp= $reponseform->getValue('descriptionhelp');
    			 
    			 
    			$result = $row->save();
    			 
    				//$reponseform->reset();
    			 
    			 $this->_helper->redirector("editerrepense","gsondage","sondage",array("id"=>$this->getRequest()->getParam("id")));
    		}
    	}
    		 
    	
    }

    public function getresultsAction()
    {
    	// action body
    	
    }

    public function mesgagniantsAction()
    {
        // action body
        
    	$id=$this->_request->getParam("id");
    	
    	$mesgagnants=new Application_Model_Mapper_Lesgagnants();
    	$listgagnant=$mesgagnants->hasGaganant($id);
    	if(count($listgagnant)>0){
    		
    		$paginator = Zend_Paginator::factory($listgagnant);
    		 
    		//ajouter le nombre d'enregistrement par page
    		$paginator->setItemCountPerPage(5);
    		//ajouter la page par defaut
    		$paginator->setCurrentPageNumber($this->_getParam('page', 1));
    		//ajouter le paginateur au view
    		$this->view->paginator = $paginator;
    		
    		
    		$headercolums=array(
    				"nom"=> "Nom",
    				"prenom"=>"Prenom",
    				"email"=>"E-mail",
    				"titre"=>"Titre Sondage",
    				"scorefinal"=>"Score final",
    				"telephone"=>"téléphone"
    		
    		);
    		$this->view->headercolums=$headercolums;
    		
    		Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
    		 
    		
    		if ($this->_request->isPost()) {
    			
    			$this->_helper->redirector("gestionsondage");
    		
    		}
    		
    	}else{
    		$sondage=new Application_Model_Mapper_Sondage();
    		 
    		$result=$sondage->getSondageResult($id);
    		
    		$resval=0;
    		$nbrres=0;
    		$resfinal=array();
    		$niveau=-1;
    		foreach($result as $res){
    			if($resval != $res['scorefinal']){
    				$resval=$res['scorefinal'];
    				$resfinal[++$niveau][]=$res;
    				if($nbrres>=3){break;}	
    			}else if($resval==$res['scorefinal']){
    				$resfinal[$niveau][]=$res;
    			}
    		$nbrres++;
    		}
    	
    		
    		$paginator=array();
    		for($i=0;$i<=$niveau;$i++){
	    		if($i==0 && count($resfinal[$i])>3){
	    			$indexs=array_rand($resfinal[$i],3);
	    			foreach ($indexs as $index){
	    				$paginator[]=$resfinal[$i][$index];
	    			}
	    	
	    		}elseif($i==0 && count($resfinal[$i])<=3){
	    			$paginator=$resfinal[$i];
	    		} 		
	    		elseif($i>0 && (count($resfinal[$i])>=(3-count($paginator)))){
	    			if((3-count($paginator)>1)){
	    				$indexs=array_rand($resfinal[$i],(3-count($paginator)));
	    				foreach ($indexs as $index){
	    					$paginator[]=$resfinal[$i][$index];
	    				}
	    			}elseif((3-count($paginator))==1){
	    				$index=array_rand($resfinal[$i],1);
	    				$paginator[]=$resfinal[$i][$index];
	    			}
	    			
	    			
	    		}elseif($i>0 && (count($resfinal[$i])<(3-count($paginator)))){
	    			$paginator = array_merge($paginator,$resfinal[$i]);
	    		}
    		}
    		
    		
    		$this->view->paginator = $paginator;
  		
    		$headercolums=array(
    				"nom"=> "Nom",
    				"prenom"=>"Prenom",
    				"email"=>"E-mail",
    				"titre"=>"Titre Sondage",
    				"scorefinal"=>"Score final",
    				"telephone"=>"téléphone"
    		);
    		$this->view->headercolums=$headercolums;
    		 
    		if ($this->_request->isPost()) {
    			$formData = $this->_request->getPost();
    			$gagant=new Application_Model_DbTable_Lesgagnants();
    			$gagnantres=explode("_", $formData['gagnants']);
    			foreach($gagnantres as $items){
    				$row = $gagant->createRow();
    				$row->idparticipation=$items;
    				$row->save();
    			}
    		
    			$this->_helper->redirector("gestionsondage");
    		
    		}
    	}
   	
    }


}

